<#include "/lib/lib.html">
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>指标管理</title>
    <meta name="keywords" content="">
    <meta name="description" content="">

    <link rel="shortcut icon" href="${request.contextPath}/hplus/favicon.ico">
    <link href="${request.contextPath}/hplus/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="${request.contextPath}/hplus/css/font-awesome.min.css?v=4.4.0" rel="stylesheet">
    <link href="${request.contextPath}/hplus/css/animate.min.css" rel="stylesheet">
    <link href="${request.contextPath}/hplus/css/style.css?v=4.1.0" rel="stylesheet">
    <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.11.1/bootstrap-table.min.css">
    <link rel="stylesheet" href="${request.contextPath}/hplus/css/plugins/bootstrap-validator/bootstrapValidator.min.css">
    <link rel="stylesheet" href="${request.contextPath}/hplus/css/plugins/treegrid/jquery.treegrid.css">
    <link rel="stylesheet" href="${request.contextPath}/hplus/css/plugins/zTree/metroStyle/metroStyle.css">
    <style>
        .form-control:focus,
        .single-line:focus {
            border-color: #0e9aef !important;
        }
    </style>
</head>
<body class="gray-bg">

    <div class="wrapper wrapper-content">
        <div class="ibox float-e-margins ibox-blue">
            <div class="ibox-title">
                <h5>指标管理</h5>
                <div class="ibox-tools">
                    <a class="collapse-link">
                        <i class="fa fa-chevron-up"></i>
                    </a>
                    <a class="close-link">
                        <i class="fa fa-times"></i>
                    </a>
                </div>
            </div>
            <div class="ibox-content">

                <div class="row row-lg">
                    <div class="example" style="margin-top: -10px;">
                        <div class="btn-group hidden-xs" id="toolbar" role="group">
                            <button type="button" class="btn btn-outline btn-default" data-toggle="modal" data-target="#addModal">
                                <i class="glyphicon glyphicon-plus" aria-hidden="true"></i>
                                添加
                            </button>
                            <button type="button" class="btn btn-outline btn-default" id="updateButton">
                                <i class="glyphicon glyphicon-pencil" aria-hidden="true"></i>
                                修改
                            </button>
                            <button type="button" class="btn btn-outline btn-default" id="deleteButton">
                                <i class="glyphicon glyphicon-trash" aria-hidden="true"></i>
                                删除
                            </button>
                        </div>

                        <table id="menuTable" data-mobile-responsive="true" data-click-to-select="true">
                            <thead>
                            <tr>
                                <th data-field="selectItem" data-checkbox="true"></th>
                            </tr>
                            </thead>
                        </table>

                    </div>
                </div>

            </div>
        </div>
    </div>


    <@modal "添加指标" "addModal" "addForm">
    <div class="form-group">
        <label for="lastname" class="col-sm-2 control-label">类型</label>
        <div class="col-sm-10" id="typeRadio">
            <label>
                <input type="radio" name="type" value="0" checked="">
                <span>目录</span>
            </label>
            <label>
                <input type="radio" name="type" value="1">
                <span>指标</span>
            </label>
        </div>
    </div>
    <div class="form-group pid">
        <label for="firstname" class="col-sm-2 control-label">上级目录</label>
        <div class="col-sm-10">
            <select class="form-control" name="pid">
                <option value="">--请选择--</option>
            </select>
        </div>
    </div>
    <div class="form-group">
        <label for="firstname" class="col-sm-2 control-label">指标名称</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" name="name" placeholder="请输入指标名称">
        </div>
    </div>
    <div class="form-group pid">
        <label for="firstname" class="col-sm-2 control-label">指标权重</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" name="weight" placeholder="请输入指标权重">
        </div>
    </div>
    <div class="form-group pid">
        <label for="firstname" class="col-sm-2 control-label">选项A</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" name="optionA" placeholder="请输入指标选项">
        </div>
    </div>
    <div class="form-group pid">
        <label for="firstname" class="col-sm-2 control-label">选项B</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" name="optionB" placeholder="请输入指标选项">
        </div>
    </div>
    <div class="form-group pid">
        <label for="firstname" class="col-sm-2 control-label">选项C</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" name="optionC" placeholder="请输入指标选项">
        </div>
    </div>
    <div class="form-group pid">
        <label for="firstname" class="col-sm-2 control-label">选项D</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" name="optionD" placeholder="请输入指标选项">
        </div>
    </div>
    <div class="form-group">
        <label for="firstname" class="col-sm-2 control-label">排序</label>
        <div class="col-sm-10">
            <input type="number" class="form-control" name="sort" placeholder="请输入排序">
        </div>
    </div>
    <div class="form-group">
        <label for="lastname" class="col-sm-2 control-label">描述</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" name="description" placeholder="请输入描述">
        </div>
    </div>
    <div class="form-group">
        <label for="lastname" class="col-sm-2 control-label">状态</label>
        <div class="col-sm-10">
            <div class="ui-radio ui-radio-primary">
                <label>
                    <input type="radio" name="status" value="1" checked="">
                    <span>启用</span>
                </label>
                <label>
                    <input type="radio" name="status" value="0">
                    <span>禁用</span>
                </label>
            </div>
        </div>
    </div>
    </@modal>

    <@modal "修改权限" "updateModal" "updateForm">
    <input type="hidden" name="id">
    <div class="form-group">
        <label for="lastname" class="col-sm-2 control-label">类型</label>
        <div class="col-sm-10" id="typeRadio2">
            <label>
                <input type="radio" name="type" value="0" checked="">
                <span>目录</span>
            </label>
            <label>
                <input type="radio" name="type" value="1">
                <span>指标</span>
            </label>
        </div>
    </div>
    <div class="form-group pid">
        <label for="firstname" class="col-sm-2 control-label">上级目录</label>
        <div class="col-sm-10">
            <select class="form-control" name="pid">
                <option value="">--请选择--</option>
            </select>
        </div>
    </div>
    <div class="form-group">
        <label for="firstname" class="col-sm-2 control-label">指标名称</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" name="name" placeholder="请输入指标名称">
        </div>
    </div>
    <div class="form-group pid">
        <label for="firstname" class="col-sm-2 control-label">指标权重</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" name="weight" placeholder="请输入指标权重">
        </div>
    </div>
    <div class="form-group pid">
        <label for="firstname" class="col-sm-2 control-label">选项A</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" name="optionA" placeholder="请输入指标选项">
        </div>
    </div>
    <div class="form-group pid">
        <label for="firstname" class="col-sm-2 control-label">选项B</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" name="optionB" placeholder="请输入指标选项">
        </div>
    </div>
    <div class="form-group pid">
        <label for="firstname" class="col-sm-2 control-label">选项C</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" name="optionC" placeholder="请输入指标选项">
        </div>
    </div>
    <div class="form-group pid">
        <label for="firstname" class="col-sm-2 control-label">选项D</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" name="optionD" placeholder="请输入指标选项">
        </div>
    </div>
    <div class="form-group">
        <label for="firstname" class="col-sm-2 control-label">排序</label>
        <div class="col-sm-10">
            <input type="number" class="form-control" name="sort" placeholder="请输入排序">
        </div>
    </div>
    <div class="form-group">
        <label for="lastname" class="col-sm-2 control-label">描述</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" name="description" placeholder="请输入描述">
        </div>
    </div>
    <div class="form-group">
        <label for="lastname" class="col-sm-2 control-label">状态</label>
        <div class="col-sm-10">
            <div class="ui-radio ui-radio-primary">
                <label>
                    <input type="radio" name="status" value="1" checked="">
                    <span>启用</span>
                </label>
                <label>
                    <input type="radio" name="status" value="0">
                    <span>禁用</span>
                </label>
            </div>
        </div>
    </div>
    </@modal>
    <@simpleModal "选择上级目录" "alterPermissionModal" "submitButton">
        <ul id="treeDemo" class="ztree"></ul>
    </@simpleModal>
    <!-- 全局js -->
    <script src="${request.contextPath}/hplus/js/jquery.min.js?v=2.1.4"></script>
    <script src="${request.contextPath}/hplus/js/bootstrap.min.js?v=3.3.6"></script>
    <!-- layer javascript -->
    <script src="${request.contextPath}/hplus/js/plugins/layer/layer.min.js"></script>
    <!-- 自定义js -->
    <script src="${request.contextPath}/hplus/js/content.js?v=1.0.0"></script>
    <!-- Latest compiled and minified JavaScript -->
    <script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.11.1/bootstrap-table.min.js"></script>
    <!-- Latest compiled and minified Locales -->
    <script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.11.1/locale/bootstrap-table-zh-CN.min.js"></script>
    <script src="${request.contextPath}/hplus/js/plugins/bootstrap-validator/bootstrapValidator.js"></script>
    <!-- treegrid -->
    <script src="${request.contextPath}/hplus/js/plugins/treegrid/jquery.treegrid.min.js"></script>
    <script src="${request.contextPath}/hplus/js/plugins/treegrid/jquery.treegrid.bootstrap3.js"></script>
    <script src="${request.contextPath}/hplus/js/plugins/treegrid/jquery.treegrid.extension.js"></script>
    <script src="${request.contextPath}/hplus/js/plugins/treegrid/tree.table.js"></script>
    <script type="text/javascript" src="${request.contextPath}/hplus/js/plugins/zTree/jquery.ztree.core.min.js"></script>
    <script type="text/javascript" src="${request.contextPath}/hplus/js/plugins/zTree/jquery.ztree.excheck.min.js"></script>

    <script>
        var Menu = {
            id: "menuTable",
            table: null,
            layerIndex: -1
        };

        /**
         * 初始化表格的列
         */
        Menu.initColumn = function () {
            var columns = [
                {
                    field: 'selectItem',
                    radio: true
                },
                {
                    field: 'id',
                    title: 'ID',
                    align: 'center',
                    valign: 'middle',
                    width: '60px'
                },
                {
                    field: 'name',
                    title: '指标名称',
                    align: 'center',
                    valign: 'middle'
                },
                {
                    field: 'type',
                    title: '类型',
                    align: 'center',
                    valign: 'middle',
                    formatter: function(item, index) {
                        if(item.type === 0){
                            return '<span class="label label-primary">目录</span>';
                        }
                        if(item.type === 1){
                            return '<span class="label label-success">指标</span>';
                        }
                    }
                },
                {
                    field: 'sort',
                    title: '排序号',
                    align: 'center',
                    valign: 'middle'
                },
                {
                    field: 'status',
                    title: '状态',
                    align: 'center',
                    valign: 'middle',
                    formatter: function(item, index){
                        if (item.status == 0) {
                            return '<span class="label label-danger">禁用</span>';
                        } else if (item.status == 1) {
                            return '<span class="label label-primary">启用</span>';
                        }
                        return "<span>其他</span>";
                    }
                },
                {
                    field: 'gmtCreate',
                    title: '创建时间',
                    align: 'center',
                    formatter:function(item, index){
                        return formatDateTime(item.gmtCreate);
                    }
                }
                ];
            return columns;
        };

        $(function () {
            var colunms = Menu.initColumn();
            var table = new TreeTable(Menu.id, "/admin/indicator/getList", colunms);
            table.setExpandColumn(2);
            table.setIdField("id");
            table.setCodeField("id");
            table.setParentCodeField("pid");
            table.setExpandAll(false);
            table.init();
            Menu.table = table;
        });

        function getMenuId () {
            var selected = $('#menuTable').bootstrapTreeTable('getSelections');
            if (selected.length == 0) {
                parent.layer.msg('请选择一条记录', {time: 1500, icon:5});
                return false;
            } else {
                return selected[0].id;
            }
        }

        //添加指标
        $('#addModal').on('show.bs.modal', function () {
            $("#addForm").data('bootstrapValidator').destroy();
            $('#addForm').data('bootstrapValidator', null);
            addValidator();
        });
        addValidator();
        function addValidator() {
            $('#addForm').bootstrapValidator({
                fields: {
                    name: {
                        validators: {
                            notEmpty: {
                                message: '指标名称不能为空'
                            }
                        }
                    },
                    weight: {
                        validators: {
                            notEmpty: {
                                message: '指标权重不能为空'
                            }
                        }
                    },
                    sort: {
                        validators: {
                            notEmpty: {
                                message: '排序不能为空'
                            }
                        }
                    },
                    description: {
                        validators: {
                            notEmpty: {
                                message: '描述不能为空'
                            }
                        }
                    }
                }
            }).on('success.form.bv', function(e) {
                // Prevent form submission
                e.preventDefault();
                $.ajax({
                    url: "/admin/indicator/addIndicator",
                    data:  $("#addForm").serialize(),
                    type: "POST",
                    dataType: 'json',
                    success: function(data){
                        $("#addModal").modal('hide');
                        if (data.success) {
                            $("#addModal").modal('hide');
                            $("#addForm")[0].reset();
                            $("#addModal .pid").hide();
                            Menu.table.refresh();
                            parent.layer.msg(data.msg, {icon: 1});
                        }
                    }
                });
            });
        }

        //修改指标
        $("#updateButton").click(function () {
            var id = getMenuId();
            if (id == false) {
                return false;
            }
            var list = [];
            $.ajax({
                async:false,
                url: "/admin/indicator/getIndicatorById",
                data: {"id": id},
                type: "POST",
                dataType: 'json',
                success: function(data){
                    list = data;
                }
            });
            for (var item in list) {
                if (item == 'status' || item == 'type') {
                    continue;
                }
                $("#updateModal input[name='"+item+"']").val(list[item]);
            }
            if (list.type == 0) {
                $("#updateModal .pid").hide();
            } else if (list.type == 1) {
                setDomPermistion(list.pid);
                $("#updateModal .pid").show();
            }
            $("#updateModal input[name='type'][value="+list.type+"]").prop("checked",true);
            $("#updateModal input[name='status'][value="+list.status+"]").prop("checked",true);
            $("#updateForm").data('bootstrapValidator').destroy();
            $('#updateForm').data('bootstrapValidator', null);
            updateValidator();
            $("#updateModal").modal("show");
        });
        updateValidator();
        function updateValidator() {
            $('#updateForm').bootstrapValidator({
                fields: {
                    name: {
                        validators: {
                            notEmpty: {
                                message: '指标名称不能为空'
                            }
                        }
                    },
                    weight: {
                        validators: {
                            notEmpty: {
                                message: '指标权重不能为空'
                            }
                        }
                    },
                    sort: {
                        validators: {
                            notEmpty: {
                                message: '排序不能为空'
                            }
                        }
                    },
                    description: {
                        validators: {
                            notEmpty: {
                                message: '描述不能为空'
                            }
                        }
                    }
                }
            }).on('success.form.bv', function(e) {
                // Prevent form submission
                e.preventDefault();
                $.ajax({
                    url: "/admin/indicator/updateIndicator",
                    data:  $("#updateForm").serialize(),
                    type: "POST",
                    dataType: 'json',
                    success: function(data){
                        $("#updateModal").modal('hide');
                        if (data.success) {
                            $("#updateModal").modal('hide');
                            Menu.table.refresh();
                            parent.layer.msg(data.msg, {icon: 1});
                        }
                    }
                });
            });
        }

        $("#deleteButton").click(function () {
            var id = getMenuId();
            parent.layer.confirm('确定删除选中指标？', {
                btn: ['确定','取消'], //按钮
                shade: false //不显示遮罩
            }, function() {
                $.ajax({
                    url: "/admin/indicator/delete",
                    data:  {"id": id},
                    type: "POST",
                    dataType: 'json',
                    success: function(data){
                        if (data.success) {
                            $('#userTableEvents').bootstrapTable('refresh');
                            parent.layer.msg(data.msg, {icon: 1});
                            Menu.table.refresh();
                        } else {
                            parent.layer.msg(data.msg, {time: 1500, icon:5});
                        }
                    }
                });
            });
        });

        $("#addModal .pid").hide();
        $("#addModal #typeRadio input:radio").click(function () {
            if (this.value == 0) {
                $("#addModal .pid").hide();
            } else if (this.value == 1) {
                $("#addModal .pid").show();
            }
        });


        $("#updateModal #typeRadio2 input:radio").click(function () {
            if (this.value == 0) {
                $("#updateModal .pid").hide();
            } else if (this.value == 1) {
                $("#updateModal .pid").show();
            }
        });

        function setDomPermistion(pid) {
            $("#addModal select, #updateModal select").empty();
            var domOption = "";
            $.ajax({
                async:false,
                url: "/admin/indicator/getIndicatorById",
                data: {"id": pid},
                type: "POST",
                dataType: 'json',
                success: function(result){
                    domOption += "<option value='"+result.id+"' selected>"+result.name+"</option>";
                }
            });
            $("#addModal select, #updateModal select").append(domOption);
        }
        //上级指标
        var setting = {
            callback: {
                beforeExpand: false
            },
            check: {
                enable: true,
                chkStyle: "radio",
                radioType: "all"
            },
            view: {
                expandSpeed: "slow"
            },
            data: {
                simpleData: {
                    enable: true,
                    idKey: "id",
                    pIdKey: "pid",
                    rootPId: 0
                }
            }
        };
        $("select").focus(function () {
            var pid = $("select[name='pid']").val();
            $("#addModal select, #updateModal select").empty();
            debugger
            var zNodes = [];
            $.ajax({
                async : false,
                url: "/admin/indicator/getIndicatorZTreeNodes",
                data:  {"pid":pid},
                type: "POST",
                dataType: 'json',
                success: function(data){
                    zNodes = data;
                }
            });
            $.fn.zTree.init($("#treeDemo"), setting, zNodes);
            $("#alterPermissionModal").modal("show");
        })
        $("#submitButton").click(function () {
            var zTree = $.fn.zTree.getZTreeObj("treeDemo");
            nodes = zTree.getCheckedNodes(true);
            if (nodes.length <= 0) {
                parent.layer.msg('请至少选中一个节点', {time: 1500, icon:5});
                return;
            }
            var domOption = "<option value='"+nodes[0].id+"' selected>"+nodes[0].name+"</option>";
            $("#addModal select, #updateModal select").append(domOption);
            $("#alterPermissionModal").modal("hide");
        })
    </script>
</body>
</html>
